﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Jacobi.Vst.Core;
using Mvvm;
using Play.Messaging;
using Play.TrackManagement;
using Play.VstPluginManagement;
using Utils;

namespace Play.Shell.ViewModels
{
	public class SelectInstrumentViewModel : ViewModelBase
	{
		public SelectInstrumentViewModel(VstPluginCatalog pluginCatalog)
		{
			BackCommand = new RelayCommand(Back);

			Plugins = pluginCatalog.Plugins
				.Where(it => it.Category == VstPluginCategory.Synth)
				.Select(it => new VstPluginSelectViewModel(it.PluginName, () => Select(it)))
				.ToList();
		}

		public event MessageEventHandler SendMessage;

		public ICommand BackCommand { get; private set; }

		public IEnumerable<VstPluginSelectViewModel> Plugins { get; private set; }

		private void Back()
		{
			SendMessage(new ShowTrackViewRequestMessage());
		}

		private void Select(VstPluginInfo vstPluginInfo)
		{
			SendMessage(new CreateTrackRequestMessage(vstPluginInfo));
			Back();
		}
	}
}
